package actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import db.dao.Dept;
import db.dao.DeptDAO;
import db.dao.Teacher;
import db.dao.TeacherDAO;
import hibernate.HibernateSessionFactory;
/*修改教师信息*/
public class UpdateTeacherAction extends ActionSupport{
	private String tid="";	//教师编号
	private String tname="";	//教师姓名
	private String tsex="";	//教师性别
	private String tiden="";	//教师身份证号
	private String tdept="";	//教师所属学院
	
	public UpdateTeacherAction() {
	}

	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
	}

	public String getTname() {
		return tname;
	}

	public void setTname(String tname) {
		this.tname = tname;
	}

	public String getTsex() {
		return tsex;
	}

	public void setTsex(String tsex) {
		this.tsex = tsex;
	}

	public String getTiden() {
		return tiden;
	}

	public void setTiden(String tiden) {
		this.tiden = tiden;
	}
	
	public String getTdept() {
		return tdept;
	}

	public void setTdept(String tdept) {
		this.tdept = tdept;
	}
	
	public String execute() throws IOException{
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		//用于获取结果
		String result="";
		//查找教师
		Teacher teacher = new Teacher();
		TeacherDAO tDao = new TeacherDAO();
		teacher = tDao.findById(tid);
		//判断是否查找到
		if (teacher!=null) {
			//对每一项判空，不为空则修改
			if (!tdept.equals("")) {
				DeptDAO dDao = new DeptDAO();
				Dept dept = new Dept();
				dept = dDao.findById(tdept);
				teacher.setDept(dept);
			}
			if (!tname.equals("")) {
				teacher.setTeaname(tname);
			}
			if (!tiden.equals("")) {
				teacher.setTeaidentify(tiden);
			}
			if (!tsex.equals("")) {
				teacher.setTeasex(tsex);
			}
			//保存
			result = tDao.save(teacher);
			if (result.equals("success")) {
				//成功弹窗提示
				response.setContentType("text/html;charset=UTF-8");
	            response.setCharacterEncoding("UTF-8");
	            PrintWriter printWriter = response.getWriter();
	            printWriter.print("<script>alert('修改成功!')</script>");
	            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
	            printWriter.flush();
	            printWriter.close();
	            response.setHeader("Pragma","No-cache");   
	            response.setHeader("Cache-Control","no-cache");  
	            response.setHeader("Cache-Control", "no-store");  
	            response.setDateHeader("Expires", 0);
				return null;
			}else {
				//失败弹窗提示
				response.setContentType("text/html;charset=UTF-8");
	            response.setCharacterEncoding("UTF-8");
	            PrintWriter printWriter = response.getWriter();
	            printWriter.print("<script>alert('修改失败!')</script>");
	            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
	            printWriter.flush();
	            printWriter.close();
	            response.setHeader("Pragma","No-cache");   
	            response.setHeader("Cache-Control","no-cache");  
	            response.setHeader("Cache-Control", "no-store");  
	            response.setDateHeader("Expires", 0);
				return null;
			}
		}else {
			//为查询到该教师，弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('未查询到该教师!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
